#!/bin/bash
#Date 2022/1/22
#mail it-arch
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
clear
echo "##########################################"
echo "#			                       #"
echo "#   Auto compile install cronolog        #"
echo "#			                       #"
echo "##########################################"
echo "1 Install cronolog-1.6.2"
echo "2 EXIT"
read -p "Please input your choice:" I
if [ "$I" == "1" ];then
        URL="ftp://ftp.netbsd.org/pub/pkgsrc/distfiles/cronolog-1.6.2.tar.gz"
	VER="cronolog-1.6.2"
elif [ "$I" == "2" ];then
        echo "you choce channel!"
        exit 1;
else
        echo "input Error! Place input{1|2}"
        exit 0;
fi
read -p "Please input php install dirname:(example:/data/php)" DIR
if [ ! -d $DIR ];then
        mkdir -p $DIR
fi
if [ $? -eq 1 ];then
        echo "$DIR fail! Please intput ture dirname."
        exit 1
fi
clear
echo -e "\033[32m You choice install $VER.Install dirname:$DIR \033[0m"
echo -e "\033[32m Press any key to start install $VER... \033[0m"
read -n 1

which tar >/dev/null 2>&1
if [ $? -eq 0 ];then
	echo "tar is installed">/tmp/php-install.log
else
	yum install tar -y >/dev/null 2>&1
fi
TDIR="/tools"
yum install -y wget gcc gcc-c++ bison git ntpdate
ntpdate ntpdate time1.aliyun.com >/dev/null 2>&1
if [ ! -d $TDIR ];then
        /bin/mkdir $TDIR && cd $TDIR
fi
/usr/bin/wget -c -O $TDIR/$VER.tar.gz $URL
if [ $? -eq 0 ];then
        tar -zxvf $TDIR/$VER.tar.gz -C $TDIR
else
        yum install tar -y >/dev/null 2>&1
        tar -zxvf $TDIR/$VER.tar.gz >/dev/null 2>&1
fi
cd  $TDIR/$VER
if [ $? -eq 0 ];then
	./configure --prefix=$DIR --exec-prefix=$DIR --bindir=$DIR/bin --sbindir=$DIR/sbin --datadir=$DIR/data	
fi
make && make install >>/tmp/cronolog-install.log 2>&1
#ln -s $DIR/bin /usr/bin
echo "export PATH=$PATH:$DIR/sbin">>/etc/profile  && source /etc/profile
cronolog --version
STAT=`echo $?`
if [ $STAT -eq 0 ];then
		echo -e "\033[32m $VER is install success! \033[0m"
else
		echo -e "\033[32m $VER install file.please check /tmp/cronolog-install.log \033[0m"
fi
